/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.Panel;

import Controller.GetGoogleMap;
import Controller.satellitalImageController;
import Entity.ReforestationZone;
import Entity.SatellitalImage;
import Interface.Windows.AreaControl;
import Interface.Windows.MainWindow;
import com.itextpdf.text.Image;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Nicolas
 */
public class PanelReforestationZoneRegister extends javax.swing.JPanel {

    /**
     * Creates new form PanelReforestationZoneRevision
     */
    private SatellitalImage satellitalImage;
    private PanelPeasantRegister parent;
    private MainWindow mainWindow;
    private EntityManagerFactory emf;
    
    public PanelReforestationZoneRegister(PanelPeasantRegister parent, MainWindow mainWindow, EntityManagerFactory emf) {
        initComponents();
        this.parent = parent;
        this.mainWindow = mainWindow;
        this.emf = emf;
    }
    
    public PanelReforestationZoneRegister() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        logoLabel2 = new javax.swing.JLabel();
        titleLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        coorXTF = new javax.swing.JTextField();
        coorYTF = new javax.swing.JTextField();
        zoomTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        seleccionarZonaBtn = new javax.swing.JButton();
        limpiarBtn = new javax.swing.JButton();
        registerZoneBtn = new javax.swing.JButton();

        jTextField1.setText("jTextField1");

        logoLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/predio.png"))); // NOI18N

        titleLabel2.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        titleLabel2.setText("Registro de Zona ULATA");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(logoLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleLabel2)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(titleLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Zona"));

        jLabel1.setText("Coordenada X:");

        jLabel2.setText("Coordenada Y:");

        coorXTF.setEditable(false);
        coorXTF.setText("0");

        coorYTF.setEditable(false);
        coorYTF.setText("0");

        zoomTF.setEditable(false);
        zoomTF.setText("0");

        jLabel3.setText("Zoom:");

        seleccionarZonaBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/1353187252_map.png"))); // NOI18N
        seleccionarZonaBtn.setText("Buscar en el Mapa");
        seleccionarZonaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seleccionarZonaBtnActionPerformed(evt);
            }
        });

        limpiarBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Clean.png"))); // NOI18N
        limpiarBtn.setText("Limpiar");
        limpiarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limpiarBtnActionPerformed(evt);
            }
        });

        registerZoneBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Save.png"))); // NOI18N
        registerZoneBtn.setText("Registrar Zona");
        registerZoneBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerZoneBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(seleccionarZonaBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(registerZoneBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(coorYTF, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(coorXTF, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(43, 43, 43)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(zoomTF, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(limpiarBtn)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(coorXTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zoomTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(limpiarBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(coorYTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(seleccionarZonaBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(registerZoneBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void registerZoneBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerZoneBtnActionPerformed
        if(!coorXTF.getText().equals("0") && !coorYTF.getText().equals("o") && !zoomTF.getText().equals("o")){
            ReforestationZone zone = new ReforestationZone();
            
            zone.setLatitude(Double.parseDouble(coorXTF.getText()));
            zone.setLongitude(Double.parseDouble(coorYTF.getText()));
            zone.setZoom(Integer.parseInt(zoomTF.getText()));
            
            satellitalImageController satellitalImageController = new satellitalImageController();
            
            String discriminator = coorXTF.getText() + coorYTF.getText() + zoomTF.getText();
            
            satellitalImage = satellitalImageController.createSatellitalImage(discriminator);
            List<SatellitalImage> satellitalList = new ArrayList<>();
            satellitalList.add(satellitalImage);
            
            zone.setSatellitalImages(satellitalList);
            parent.addZone(zone);
            parent.reloadZonesList();
            
            mainWindow.setPanel(parent);
        }else{
            JOptionPane.showMessageDialog(new JFrame(), "Error Con el Registro de Zona", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_registerZoneBtnActionPerformed

    private void limpiarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limpiarBtnActionPerformed
        setSatellitalImageData("0", "0", "0");
    }//GEN-LAST:event_limpiarBtnActionPerformed

    private void seleccionarZonaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seleccionarZonaBtnActionPerformed
        AreaControl ac = new AreaControl(this);
        ac.setLocationRelativeTo(null);
        ac.setVisible(true);
    }//GEN-LAST:event_seleccionarZonaBtnActionPerformed

    public void setSatellitalImageData(String coorx, String coory, String zoom){
        coorXTF.setText(coorx);
        coorYTF.setText(coory);
        zoomTF.setText(zoom);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField coorXTF;
    private javax.swing.JTextField coorYTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton limpiarBtn;
    private javax.swing.JLabel logoLabel2;
    private javax.swing.JButton registerZoneBtn;
    private javax.swing.JButton seleccionarZonaBtn;
    private javax.swing.JLabel titleLabel2;
    private javax.swing.JTextField zoomTF;
    // End of variables declaration//GEN-END:variables
}
